home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / MPW etc / MPW-GM / Interfaces&Libraries / Interfaces / CIncludes / Math64.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-10  |  12.2 KB  |  401 lines  |  [TEXT/MPS ]

  1. /*
  2.      File:        Math64.h
  3.  
  4.      Contains:    64-bit integer math Interfaces.
  5.  
  6.      Version:    Technology:    System 7.5
  7.                  Release:    Update to Universal Interfaces 3.2
  8.  
  9.      Copyright:    © 1994-1998 by Apple Computer, Inc., all rights reserved
  10.  
  11.      Bugs?:        For bug reports, consult the following page on
  12.                  the World Wide Web:
  13.  
  14.                      http://developer.apple.com/bugreporter/
  15.  
  16. */
  17. #ifndef __MATH64__
  18. #define __MATH64__
  19.  
  20. #ifndef __CONDITIONALMACROS__
  21. #include <ConditionalMacros.h>
  22. #endif
  23. #ifndef __MACTYPES__
  24. #include <MacTypes.h>
  25. #endif
  26.  
  27.  
  28.  
  29. #if PRAGMA_ONCE
  30. #pragma once
  31. #endif
  32.  
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36.  
  37. #if PRAGMA_IMPORT
  38. #pragma import on
  39. #endif
  40.  
  41. #if PRAGMA_STRUCT_ALIGN
  42.     #pragma options align=mac68k
  43. #elif PRAGMA_STRUCT_PACKPUSH
  44.     #pragma pack(push, 2)
  45. #elif PRAGMA_STRUCT_PACK
  46.     #pragma pack(2)
  47. #endif
  48.  
  49.  
  50. /*--------------------------------------------------------------------------------
  51.                 These routines are intended to provide C software support for
  52.                 64 bit integer types.  Their behavior should mimic anticipated
  53.                 64 bit hardware. This implementation should replace use of the
  54.                 "wide" type found in PowerPC.
  55.  
  56.     The following routines are available for performing math on 64-bit integers:
  57.     
  58.     S64Max
  59.                 Returns the largest representable SInt64.
  60.     S64Min
  61.                 Returns the smallest (i.e. most negative) SInt64.  Note: the negative
  62.                 (absolute value) of this number is not representable in an SInt64.
  63.                 That means that S64Negate(S64Min) is not representable (in fact,
  64.                 it returns S64Min).
  65.     S64Add
  66.                 Adds two integers, producing an integer result.  If an overflow
  67.                 occurs the result is congruent mod (2^64) as if the operands and
  68.                 result were unsigned.  No overflow is signaled.
  69.     
  70.     S64Subtract
  71.                 Subtracts two integers, producing an integer result.  If an overflow
  72.                 occurs the result is congruent mod (2^64) as if the operands and
  73.                 result were unsigned.  No overflow is signaled.
  74.  
  75.     S64Negate
  76.                 Returns the additive inverse of a signed number (i.e. it returns
  77.                 0 - the number).  S64Negate (S64Min) is not representable (in fact,
  78.                 it returns S64Min).
  79.     
  80.     S64Absolute
  81.                 Returns the absolute value of the number (i.e. the number if
  82.                 it is positive, or 0 - the number if it is negative).
  83.                 See S64Negate above.
  84.                 
  85.     S64Multiply
  86.                 Multiplies two signed numbers, producing a signed result.  Overflow
  87.                 is ignored and the low-order part of the product is returned.  The
  88.                 sign of the result is not guaranteed to be correct if the magnitude
  89.                 of the product is not representable.
  90.                 
  91.     S64Div
  92.                 Divides dividend by divisor, returning the quotient.
  93.                 
  94.     S64Mod
  95.                 Returns the remainder of divide of dividend by divisor.  The sign of
  96.                 the remainder is the same as the sign of the dividend (i.e., it takes
  97.                 the absolute values of the operands, does the division, then fixes
  98.                 the sign of the quotient and remainder).
  99.  
  100.     S64Divide
  101.                 Divides dividend by divisor, returning the quotient.  The remainder
  102.                 is returned in *remainder if remainder (the pointer) is non-NULL.
  103.                 The sign of the remainder is the same as the sign of the dividend
  104.                 (i.e. it takes the absolute values of the operands, does the division,
  105.                 then fixes the sign of the quotient and remainder).  If the divisor
  106.                 is zero, then S64Max() will be returned (or S64Min() if the dividend
  107.                 is negative), and the remainder will be the dividend; no error is
  108.                 reported.
  109.     
  110.     S64Set
  111.                 Given an SInt32, returns an SInt64 with the same value.  Use this
  112.                 routine instead of coding 64-bit constants (at least when the
  113.                 constant will fit in an SInt32).
  114.     
  115.     S64SetU
  116.                 Given a UInt32, returns a SInt64 with the same value.
  117.                 
  118.     S64Set
  119.                 Given an SInt64, returns an SInt32 by discarding the high-order
  120.                 32 bits.
  121.     
  122.     S64Compare
  123.                 Given two signed numbers, left and right, returns an
  124.                 SInt32 that compares with zero the same way left compares with
  125.                 right.  If you wanted to perform a comparison on 64-bit integers
  126.                 of the form:
  127.                         operand_1 <operation> operand_2
  128.                 then you could use an expression of the form:
  129.                         xxxS64Compare(operand_1,operand_2) <operation> 0
  130.                 to test for the same condition.
  131.                 
  132.                 CAUTION: DO NOT depend on the exact value returned by this routine.
  133.                 Only the sign (i.e. positive, zero, or negative) of the result is
  134.                 guaranteed.
  135.  
  136.     S64And, S64Or, S64Eor and S64Not
  137.     
  138.                 Return Boolean (1 or 0) depending on the outcome of the logical
  139.                 operation.
  140.  
  141.     S64BitwiseAnd, S64BitwiseOr, S64BitwiseEor and S64BitwiseNot
  142.     
  143.                 Return the Bitwise result.
  144.                 
  145.     S64ShiftRight and S64ShiftLeft
  146.     
  147.                 The lower 7 bits of the shift argument determines the amount of 
  148.                 shifting.  S64ShiftRight is an arithmetic shift while U64ShiftRight
  149.                 is a logical shift.
  150.  
  151.     SInt64ToLongDouble
  152.                 
  153.                 Converts SInt64 to long double.  Note all SInt64s fit exactly into 
  154.                 long doubles, thus, the binary -> decimal conversion routines
  155.                 in fp.h can be used to achieve SInt64 -> long double -> decimal
  156.                 conversions.
  157.                 
  158.     LongDoubleToSInt64
  159.     
  160.                 Converts a long double to a SInt64.  Any decimal string that fits
  161.                 into a SInt64 can be converted exactly into a long double, using the
  162.                 conversion routines found in fp.h.  Then this routine can be used
  163.                 to complete the conversion to SInt64.
  164.                 
  165.     SInt64ToWide
  166.     
  167.                 Converts a SInt64 to a wide struct.  If SInt64 is implemented
  168.                 as a typedef of wide, the marco does nothing. If SInt64 is 
  169.                 implememnted as a long long, it casts the long long into a 
  170.                 wide struct.
  171.     
  172.     WideToSInt64
  173.     
  174.                 Converts a wide struct into a SInt64.  If SInt64 is implemented
  175.                 as a typedef of wide, the marco does nothing. If SInt64 is 
  176.                 implememnted as a long long, it reads the struct into a long long.
  177.     
  178.     
  179.     The corresponding UInt64 routines are also included.
  180.     
  181. --------------------------------------------------------------------------------*/
  182.  
  183.  
  184. #if TYPE_LONGLONG 
  185.  
  186. #ifdef __MRC__
  187.     #define S64Max() 9223372036854775807LL
  188. #else
  189.     #define S64Max() 9223372036854775807
  190. #endif
  191. #define S64Min() (-S64Max() - 1)
  192. #define S64Add(x, y) ((SInt64) (x) + (SInt64) (y))
  193. #define S64Subtract(x, y) ((SInt64) (x) - (SInt64) (y))
  194. #define S64Negate(x) (-(SInt64) (x))
  195. #define S64Absolute(x) absll((SInt64) (x))
  196. #define S64Multiply(x, y) ((SInt64) (x) * (SInt64) (y))
  197. #define S64Div(x, y) ((SInt64) (x) / (SInt64) (y))
  198. #define S64Mod(x, y) ((SInt64) (x) % (SInt64) (y))
  199. #define S64Set(x) ((SInt64) (x))
  200. #define S64SetU(x) ((SInt64) (x))
  201. #define S32Set(x) ((SInt32) (x))
  202.  
  203. #define S64And(x, y) ((Boolean)((SInt64) (x) && (SInt64) (y)))
  204. #define S64Or(x, y) ((Boolean)((SInt64) (x) || (SInt64) (y)))
  205. #define S64Eor(x, y) ((Boolean)((SInt64) (x) ^ (SInt64) (y)))
  206. #define S64Not(x) ((Boolean)(!(SInt64) (x)))
  207. #define S64BitwiseAnd(x, y) ((SInt64) (x) & (SInt64) (y))
  208. #define S64BitwiseOr(x, y) ((SInt64) (x) | (SInt64) (y))
  209. #define S64BitwiseEor(x, y) (((SInt64) (x) & (~(SInt64) (y))) | ((~(SInt64) (x)) & (SInt64) (y)))
  210. #define S64BitwiseNot(x) (~(SInt64) (x))
  211. #define S64ShiftRight(x, y) ((SInt64) (x) >> (UInt32) (y))
  212. #define S64ShiftLeft(x, y) ((SInt64) (x) << (UInt32) (y))
  213. #define SInt64ToLongDouble(x) ((long double)(x))
  214. #define LongDoubleToSInt64(x) ((SInt64)(x))
  215.  
  216.  
  217. #ifdef __MRC__
  218.     #define U64Max() 0xffffffffffffffffULL
  219. #else
  220.     #define U64Max() 0xffffffffffffffff
  221. #endif
  222. #define U64Add(x, y) ((UInt64) (x) + (UInt64) (y))
  223. #define U64Subtract(x, y) ((UInt64) (x) - (UInt64) (y))
  224. #define U64Multiply(x, y) ((UInt64) (x) * (UInt64) (y))
  225. #define U64Div(x, y) ((UInt64) (x) / (UInt64) (y))
  226. #define U64Mod(x, y) ((UInt64) (x) % (UInt64) (y))
  227. #define U64Set(x) ((UInt64) (x))
  228. #define U64SetU(x) ((UInt64) (x))
  229. #define U32SetU(x) ((UInt32) (x))
  230.  
  231. #define U64And(x, y) ((Boolean)((UInt64) (x) && (UInt64) (y)))
  232. #define U64Or(x, y) ((Boolean)((UInt64) (x) || (UInt64) (y)))
  233. #define U64Eor(x, y) ((Boolean)((UInt64) (x) ^ (UInt64) (y)))
  234. #define U64Not(x) ((Boolean)(!(UInt64) (x)))
  235. #define U64BitwiseAnd(x, y) ((UInt64) (x) & (UInt64) (y))
  236. #define U64BitwiseOr(x, y) ((UInt64) (x) | (UInt64) (y))
  237. #define U64BitwiseEor(x, y) (((UInt64) (x) & (~(UInt64) (y))) | ((~(UInt64) (x)) & (UInt64) (y)))
  238. #define U64BitwiseNot(x) (~(UInt64) (x))
  239. #define U64ShiftRight(x, y) ((UInt64) (x) >> (UInt32) (y))
  240. #define U64ShiftLeft(x, y) ((UInt64) (x) << (UInt32) (y))
  241. #define UInt64ToLongDouble(x) ((long double)(x))
  242. #define LongDoubleToUInt64(x) ((UInt64)(x))
  243. #define UInt64ToSInt64(x) ((SInt64)(x))
  244. #define SInt64ToUInt64(x) ((UInt64)(x))
  245.  
  246. #else  
  247.  
  248. EXTERN_API_C( SInt64 ) S64Max(void );
  249.  
  250. EXTERN_API_C( SInt64 ) S64Min(void );
  251.  
  252. EXTERN_API_C( SInt64 ) S64Add(SInt64 x, SInt64 y);
  253.  
  254. EXTERN_API_C( SInt64 ) S64Subtract(SInt64 left, SInt64 right);
  255.  
  256. EXTERN_API_C( SInt64 ) S64Negate(SInt64 value);
  257.  
  258. EXTERN_API_C( SInt64 ) S64Absolute(SInt64 value);
  259.  
  260. EXTERN_API_C( SInt64 ) S64Multiply(SInt64 xparam, SInt64 yparam);
  261.  
  262. #define S64Div(dividend, divisor) S64Divide(dividend, divisor, NULL)
  263.  
  264. EXTERN_API_C( SInt64 ) S64Mod(SInt64 dividend, SInt64 divisor);
  265.  
  266. EXTERN_API_C( SInt64 ) S64Divide(SInt64 dividend, SInt64 divisor, SInt64 *remainder);
  267.  
  268. EXTERN_API_C( SInt64 ) S64Set(SInt32 value);
  269.  
  270. EXTERN_API_C( SInt64 ) S64SetU(UInt32 value);
  271.  
  272. EXTERN_API_C( SInt32 ) S32Set(SInt64 value);
  273.  
  274. EXTERN_API_C( Boolean ) S64And(SInt64 left, SInt64 right);
  275.  
  276. EXTERN_API_C( Boolean ) S64Or(SInt64 left, SInt64 right);
  277.  
  278. EXTERN_API_C( Boolean ) S64Eor(SInt64 left, SInt64 right);
  279.  
  280. EXTERN_API_C( Boolean ) S64Not(SInt64 value);
  281.  
  282. EXTERN_API_C( SInt64 ) S64BitwiseAnd(SInt64 left, SInt64 right);
  283.  
  284. EXTERN_API_C( SInt64 ) S64BitwiseOr(SInt64 left, SInt64 right);
  285.  
  286. EXTERN_API_C( SInt64 ) S64BitwiseEor(SInt64 left, SInt64 right);
  287.  
  288. EXTERN_API_C( SInt64 ) S64BitwiseNot(SInt64 value);
  289.  
  290. EXTERN_API_C( SInt64 ) S64ShiftRight(SInt64 value, UInt32 shift);
  291.  
  292. EXTERN_API_C( SInt64 ) S64ShiftLeft(SInt64 value, UInt32 shift);
  293.  
  294. /*
  295.     "long double" means 128 bit type on PowerPC and 80-bit type on 68K
  296. */
  297. EXTERN_API_C( long double ) SInt64ToLongDouble(SInt64 value);
  298.  
  299. EXTERN_API_C( SInt64 ) LongDoubleToSInt64(long double value);
  300.  
  301. EXTERN_API_C( UInt64 ) U64Max(void );
  302.  
  303. EXTERN_API_C( UInt64 ) U64Add(UInt64 x, UInt64 y);
  304.  
  305. EXTERN_API_C( UInt64 ) U64Subtract(UInt64 left, UInt64 right);
  306.  
  307. EXTERN_API_C( UInt64 ) U64Multiply(UInt64 xparam, UInt64 yparam);
  308.  
  309. #define U64Div(dividend, divisor) U64Divide(dividend, divisor, NULL)
  310.  
  311. EXTERN_API_C( UInt64 ) U64Mod(UInt64 dividend, UInt64 divisor);
  312.  
  313. EXTERN_API_C( UInt64 ) U64Divide(UInt64 dividend, UInt64 divisor, UInt64 *remainder);
  314.  
  315. EXTERN_API_C( UInt64 ) U64Set(SInt32 value);
  316.  
  317. EXTERN_API_C( UInt64 ) U64SetU(UInt32 value);
  318.  
  319. EXTERN_API_C( UInt32 ) U32SetU(UInt64 value);
  320.  
  321. EXTERN_API_C( Boolean ) U64And(UInt64 left, UInt64 right);
  322.  
  323. EXTERN_API_C( Boolean ) U64Or(UInt64 left, UInt64 right);
  324.  
  325. EXTERN_API_C( Boolean ) U64Eor(UInt64 left, UInt64 right);
  326.  
  327. EXTERN_API_C( Boolean ) U64Not(UInt64 value);
  328.  
  329. EXTERN_API_C( UInt64 ) U64BitwiseAnd(UInt64 left, UInt64 right);
  330.  
  331. EXTERN_API_C( UInt64 ) U64BitwiseOr(UInt64 left, UInt64 right);
  332.  
  333. EXTERN_API_C( UInt64 ) U64BitwiseEor(UInt64 left, UInt64 right);
  334.  
  335. EXTERN_API_C( UInt64 ) U64BitwiseNot(UInt64 value);
  336.  
  337. EXTERN_API_C( UInt64 ) U64ShiftRight(UInt64 value, UInt32 shift);
  338.  
  339. EXTERN_API_C( UInt64 ) U64ShiftLeft(UInt64 value, UInt32 shift);
  340.  
  341. /*
  342.     "long double" means 128 bit type on PowerPC and 80-bit type on 68K
  343. */
  344. EXTERN_API_C( long double ) UInt64ToLongDouble(UInt64 value);
  345.  
  346. EXTERN_API_C( UInt64 ) LongDoubleToUInt64(long double value);
  347.  
  348. EXTERN_API_C( SInt64 ) UInt64ToSInt64(UInt64 value);
  349.  
  350. EXTERN_API_C( UInt64 ) SInt64ToUInt64(SInt64 value);
  351.  
  352. #endif /* TYPE_LONGLONG */
  353.  
  354. EXTERN_API_C( SInt32 ) S64Compare(SInt64 left, SInt64 right);
  355.  
  356. EXTERN_API_C( SInt32 ) U64Compare(UInt64 left, UInt64 right);
  357.  
  358.  
  359. /* 
  360.     Functions to convert between [Unsigned]Wide and [S|U]Int64 types.
  361.     
  362.     These functions are necessary if source code which uses both
  363.     wide and SInt64 is to compile under a compiler that supports
  364.     long long.
  365. */
  366. #if TYPE_LONGLONG 
  367.     #define SInt64ToWide(x)         (*((wide*)(&x)))
  368.     #define WideToSInt64(x)         (*((SInt64*)(&x)))
  369.     #define UInt64ToUnsignedWide(x) (*((UnsignedWide*)(&x)))
  370.     #define UnsignedWideToUInt64(x) (*((UInt64*)(&x)))
  371. #else
  372.     #define SInt64ToWide(x)         (x)
  373.     #define WideToSInt64(x)         (x)
  374.     #define UInt64ToUnsignedWide(x) (x)
  375.     #define UnsignedWideToUInt64(x) (x)
  376. #endif
  377.  
  378.  
  379.  
  380.  
  381. #if PRAGMA_STRUCT_ALIGN
  382.     #pragma options align=reset
  383. #elif PRAGMA_STRUCT_PACKPUSH
  384.     #pragma pack(pop)
  385. #elif PRAGMA_STRUCT_PACK
  386.     #pragma pack()
  387. #endif
  388.  
  389. #ifdef PRAGMA_IMPORT_OFF
  390. #pragma import off
  391. #elif PRAGMA_IMPORT
  392. #pragma import reset
  393. #endif
  394.  
  395. #ifdef __cplusplus
  396. }
  397. #endif
  398.  
  399. #endif /* __MATH64__ */
  400.  
  401.